<?php
	#############################################################################################
	# Programmer: สมบูรณ์  กองลี																#
	# Email		: hugo8xx@hotmail.com.															#
	#############################################################################################
	
	if (!defined('BASE_ABS')) exit('No direct script access allowed');
	
	function splitPage_2layout($table,$filter="",$start=0,$limit=20,$page="",$field="*", $count="") {
		$page	= ($page=="") ? 1 : $page;
		if($count!=""){
			$sql  = $count;
		} else {
			$sql = ($filter!="") ? "SELECT COUNT(*) AS all_row FROM $table WHERE $filter" : "SELECT COUNT(*) AS all_row FROM $table";
		}
		$query = mysql_query($sql) or die($sql);
		while($result = mysql_fetch_array($query)) {
			$all_row = $result['all_row'];
		}
		mysql_free_result($query);
		
		if($all_row<$limit) { 
			$sql = "SELECT $field FROM $table"; 
			$sql .= ($filter!="") ? " WHERE $filter" : "";
			$sql .= " LIMIT ".((($page-1)*$limit)+($start*$page)).",$limit";
			return array($sql,1);
		}

		$all_page = ceil($all_row/($limit+$start));
		if($page>$all_page){
			$page = $all_page;
		}

		$sql = "SELECT $field FROM $table";
		$sql .= ($filter!="") ? " WHERE $filter" : "";
		$sql .= " LIMIT ".((($page-1)*$limit)+($start*$page)).",$limit";

		return array($sql,$all_page);
	}

	function splitPage_start($table,$filter="",$start=0,$limit=20,$page="",$field="*", $count="") {
		$page	= ($page=="") ? 1 : $page;
		if($count!=""){
			$sql  = $count;
		} else {
			$sql = ($filter!="") ? "SELECT COUNT(*) AS all_row FROM $table WHERE $filter" : "SELECT COUNT(*) AS all_row FROM $table";
		}
		$query = mysql_query($sql) or die($sql);
		while($result = mysql_fetch_array($query)) {
			$all_row = $result['all_row'];
		}
		mysql_free_result($query);
		
		if($all_row<$limit) { 
			$sql = "SELECT $field FROM $table"; 
			$sql .= ($filter!="") ? " WHERE $filter" : "";
			$sql .= " LIMIT ".((($page-1)*$limit)+($start)).",$limit";
			return array($sql,1);
		}

		$all_page = ceil($all_row/($limit+$start));
		if($page>$all_page){
			$page = $all_page;
		}

		$sql = "SELECT $field FROM $table";
		$sql .= ($filter!="") ? " WHERE $filter" : "";
		$sql .= " LIMIT ".((($page-1)*$limit)+($start)).",$limit";

		return array($sql,$all_page);
	}
	
	
	function getPage($p=0,$all_page=0){
		if(($p > 1) AND ($p <= $all_page)){
			return $p;
		}else{
			return 1;
		}
	}

	function start_row($p=0,$start_row=0,$limit=0,$all_page=0){
		if(($p > 1) AND ($p <= $all_page)){
			return ($start_row+$limit)*($p-1);
		}else{
			return 0;
		}
	}
?>